# Locate libusb library
# This module defines
# LIBUSB_FOUND, if false, do not try to link to libusb
# LIBUSB_INCLUDE_DIRS, where to find the headers
# LIBUSB_LIBRARIES, the libraries to link against

# 在系统中查找 libusb 库及其相关头文件的路径。
# 它使用了 CMake 的 `find_path` 命令来搜索名为 `libusb.h` 的头文件，
# 然后尝试在指定的路径下查找该头文件。如果找到了，
# 则会将路径存储在变量 `LIBUSB_INCLUDE_DIR` 中。
# 在这个例子中，搜索的路径包括 `/usr/include` 和 `/usr/local/include`，
# 并且在 `/usr/include/libusb-1.0` 和 `/usr/local/include/libusb-1.0` 
# 目录中寻找 `libusb.h` 头文件。

# PATHS 先查找系统路径,再查找定义路径
# HINTS 先查找定义路径,再查找系统路径
# NO_DEFAULT_PATH 禁用默认路径

if(CMAKE_SYSTEM_NAME MATCHES "Windows")

    # windows-x86
    find_path(LIBUSB_INCLUDE_DIR
        NAMES
        libusb-1.0/libusb.h
        HINTS
        ${CMAKE_SOURCE_DIR}/libs/include
        NO_DEFAULT_PATH
    )

    find_library(LIBUSB_LIBRARY
        NAMES
        libusb-1.0
        HINTS
        ${CMAKE_SOURCE_DIR}/libs/dll
        NO_DEFAULT_PATH
    )

    # 只查找dll文件可以使用find_file
    # find_file(LIBUSB_LIBRARY
    #     NAMES
    #     libusb-1.0.dll
    #     HINTS
    #     ${CMAKE_SOURCE_DIR}/libs/dll
    #     NO_DEFAULT_PATH
    # )

    # cpack时不允许使用绝对路径${参数}都不允许
    if(WPACK)
        install(FILES
            ${CMAKE_CURRENT_SOURCE_DIR}/libs/dll/libusb-1.0.dll
            DESTINATION ${EXEC_INSTALL_BIN}
            RENAME msys-usb-1.0.dll
        )
    else(WPACK)
        install(FILES
            ${CMAKE_CURRENT_SOURCE_DIR}/libs/dll/libusb-1.0.dll
            DESTINATION "${CMAKE_CURRENT_SOURCE_DIR}/install/sys/bin"
            RENAME msys-usb-1.0.dll
        )
    endif(WPACK)

elseif(CMAKE_SYSTEM_NAME MATCHES "Linux")

    if(CMAKE_SYSTEM_PROCESSOR MATCHES "arm")
        # arm
        find_path(LIBUSB_INCLUDE_DIR
            NAMES
            libusb-1.0/libusb.h
            PATHS
            /home/mat/workspace/C/usb/libusb-1.0.23/tmp/include
            NO_DEFAULT_PATH
        )

        find_library(LIBUSB_LIBRARY
            NAMES
            usb-1.0
            PATHS
            /home/mat/workspace/C/usb/libusb-1.0.23/tmp/lib
            NO_DEFAULT_PATH
        )
    else()
        # linux-x86
        find_path(LIBUSB_INCLUDE_DIR
            NAMES
            libusb.h
            PATH_SUFFIXES
            libusb-1.0   # PATHS目录中的libusb-1.0子目录
            PATHS
            /usr/include
            /usr/local/include
        )

        find_library(LIBUSB_LIBRARY
            NAMES
            usb-1.0
            PATH_SUFFIXES
            x86_64-linux-gnu # PATHS目录中的x86_64-linux-gnu子目录
            PATHS
            /usr/lib
            /usr/local/lib
            /lib
        )
    endif()

elseif(CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
    MESSAGE(STATUS "current platform: FreeBSD")
else()
    MESSAGE(STATUS "other platform: ${CMAKE_SYSTEM_NAME}")
endif()

if(LIBUSB_INCLUDE_DIR AND LIBUSB_LIBRARY)
    set(LIBUSB_FOUND TRUE)
else()
    set(LIBUSB_FOUND FALSE)
endif()

if(LIBUSB_FOUND)
    if(NOT Libusb_FIND_QUIETLY)
        message(STATUS "Found libusb: ${LIBUSB_LIBRARY}")
    endif()
else()
    if(Libusb_FIND_REQUIRED)
        message(FATAL_ERROR "libusb not found")
    endif()
endif()

set(LIBUSB_INCLUDE_DIRS ${LIBUSB_INCLUDE_DIR})
set(LIBUSB_LIBRARIES ${LIBUSB_LIBRARY})
